#! /bin/sh

#Goto here
here="/users/left/guillaume/bots"
cd $here

#BBC Lisbon weather id
id=$2

#BBC weather RSS feed address
feed="http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/${id}.xml"

#City
city=$1

#temporary file
file="weather${city}"

#Weather twitter bot
twitbot=login:passw

#Read the RSS feed and filter it
#curl $feed | grep 'title' | tail -n 1 | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" > $file.txt
#curl $feed | grep 'title' | perl -wlne'm/title>(.*)<\/title/i && print $1' | sed -e "s/&#xB0;//g" | tail -n 3 | head -n 1 > $file.txt
#curl $feed | awk '/<title>|<description>/' | tail -n 6 | head -n 2 | sed -e :a -e '$!N;s/\n//;ta' -e 'P;D' | sed -e 's/Max.*Max/Max/g' | sed -e 's/<\w*>//g' | sed -e "s/&#xB0;//g" | awk -F, '{print $1","$2","$3","$4","$5","$8}' > $file.txt
curl $feed | awk '/title|description/' | head -n 6 | tail -n 3 | sed -e :a -e '$!N;s/\n//;ta' -e 'P;D' | sed -e 's/Max.*Max/Max/g' | sed -e 's/<.\w*>//g' | sed -e 's/&#xB0;//g' | sed -e 's/ (\w*)//g' | awk -F, '{print $1","$2","$3","$4","$5","$8","$9","$10}' | sed -e 's/ Direction//g' | sed -e 's/, Wind Speed://gi' > $file.txt

#Timestamp the log file
echo ------------------------------------------------------->> $file.log
date >> $file.log

#Read the forecast into a weather variable
read mess < $file.txt

#Twit the weather variable away
curl --user $twitbot --data status="$mess" http://twitter.com/statuses/update.xml >> $file.log
